package com.htudata.hieyes.flow.service.impl ;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htudata.common.enums.RequestStatusEnum;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.hieyes.flow.dao.FlowDao;
import com.htudata.hieyes.flow.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FlowServiceImpl implements FlowService {

  @Autowired
  private FlowDao flowDao;
  
  @Value("${resource_monitornode}")
  private String RESOURCE_MONITORNODE;
  
  public JSONObject getMonitorNodeData(String flowId)  throws Exception{
    JSONObject monitorNodeData = this.flowDao.selectMonitorNodeData(flowId);
    JSONArray data = new JSONArray();
    if ("all".equals(flowId)) {
      JSONObject data1 = new JSONObject();
      data1.put("name", "业务流程总数");
      data1.put("value", Integer.valueOf(monitorNodeData.getIntValue("flowCount")));
      data.add(data1);
    } 
    JSONObject data2 = new JSONObject();
    data2.put("name", "指标数");
    data2.put("value", Integer.valueOf(monitorNodeData.getIntValue("itemCount")));
    data.add(data2);
    JSONObject data3 = new JSONObject();
    data3.put("name", "节点数");
    data3.put("value", Integer.valueOf(monitorNodeData.getIntValue("nodeCount")));
    data.add(data3);
    JSONObject data6 = new JSONObject();
    data6.put("name", "告警数");
    data6.put("value", Integer.valueOf(monitorNodeData.getIntValue("problemCount")));
    data.add(data6);
    if (!"all".equals(flowId)) {
      JSONObject data8 = new JSONObject();
      data8.put("name", "健康度");
      data8.put("value", Integer.valueOf(98));
      data.add(data8);
    } 
    JSONObject result = handleResult(data, this.RESOURCE_MONITORNODE);
    return result;
  }
  
  public String getMonitorNodeDataJSON(String flowId, String code) throws Exception {
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(code, getMonitorNodeData(flowId));
    } catch (Exception e) {
       throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    return result;
  }
  
  public JSONObject handleResult(Object data, String desc) {
    JSONObject result = new JSONObject(true);
    result.put("desc", desc);
    result.put("data", data);
    return result;
  }
}
